package com.creator.pangu.core.security.dto;

import com.creator.pangu.base.entity.SysPermission;
import com.creator.pangu.base.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *  登录实体类，集成springsecurity UserDeitails
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {

    @Serial
    private static final long serialVersionUID = 3607194540709851871L;

    private SysUser sysUser;

    private List<SysPermission> sysPermissionList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        Set<SimpleGrantedAuthority> grantedAuthorityList = new HashSet<>();
        if (sysPermissionList != null && sysPermissionList.size() > 0) {
            for (SysPermission sysPermission : sysPermissionList) {
                grantedAuthorityList.add(new SimpleGrantedAuthority(sysPermission.getPermissionKey()));
            }
        }
        return grantedAuthorityList;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
